/*
 * Copyright (c) 2022, IMMORTA Inc.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 * - Redistributions of source code must retain the above copyright notice, this list
 *   of conditions and the following disclaimer.
 *
 * - Redistributions in binary form must reproduce the above copyright notice, this
 *   list of conditions and the following disclaimer in the documentation and/or
 *   other materials provided with the distribution.
 *
 * - Neither the name of IMMORTA Inc. nor the names of its
 *   contributors may be used to endorse or promote products derived from this
 *   software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

/*!
 * @file mpu_drv.c
 * @brief This file defines mpu driver functions
 */

/*******Includes***************************************************************/
#include "mpu_reg_access.h"

/*******Definitions************************************************************/

/*******Variables**************************************************************/

/*******Prototypes*************************************************************/

/*******Private Functions******************************************************/

/*******Code*******************************************************************/
#if (__MPU_PRESENT == 1U)

/*******************************************************************************
 *** Function Name : MPU_Enable
 *** Description   : MPU global enable and specifies the control mode
 ******************************************************************************/
void MPU_Enable(uint32_t mpuControl)
{
    MPU_REG_Enable(MPU, mpuControl);
}

/*******************************************************************************
 *** Function Name : MPU_Disable
 *** Description   : MPU global disable
 ******************************************************************************/
void MPU_Disable(void)
{
    MPU_REG_Disable(MPU);
}

/*******************************************************************************
 *** Function Name : MPU_ConfigRegion
 *** Description   : Initialize and configure the region and the memory to be protected
 ******************************************************************************/
void MPU_ConfigRegion(mpu_region_config_t* regionConfig)
{
    /* Check the parameters */
    ASSERT_PARAM(regionConfig->Number <= MPU_REGION_NUMBER7);
    ASSERT_PARAM((regionConfig->Enable == MPU_REGION_ENABLE) ||
                 (regionConfig->Enable == MPU_REGION_DISABLE));
    ASSERT_PARAM((regionConfig->DisableExec == MPU_INSTRUCTION_ACCESS_ENABLE) ||
                 (regionConfig->DisableExec == MPU_INSTRUCTION_ACCESS_DISABLE));
    ASSERT_PARAM((regionConfig->AccessPermission == MPU_REGION_NO_ACCESS) ||
                 (regionConfig->AccessPermission == MPU_REGION_PRIV_RW) ||
                 (regionConfig->AccessPermission == MPU_REGION_PRIV_RW_URO) ||
                 (regionConfig->AccessPermission == MPU_REGION_FULL_ACCESS) ||
                 (regionConfig->AccessPermission == MPU_REGION_PRIV_RO) ||
                 (regionConfig->AccessPermission == MPU_REGION_PRIV_RO_URO));
    ASSERT_PARAM((regionConfig->TypeExtField <= MPU_TEX_LEVEL0) ||
                 (regionConfig->TypeExtField <= MPU_TEX_LEVEL1) ||
                 (regionConfig->TypeExtField <= MPU_TEX_LEVEL2));
    ASSERT_PARAM((regionConfig->IsShareable == MPU_ACCESS_SHAREABLE) ||
                 (regionConfig->IsShareable == MPU_ACCESS_NOT_SHAREABLE));
    ASSERT_PARAM((regionConfig->IsCacheable == MPU_ACCESS_CACHEABLE) ||
                 (regionConfig->IsCacheable == MPU_ACCESS_NOT_CACHEABLE));
    ASSERT_PARAM((regionConfig->IsBufferable == MPU_ACCESS_BUFFERABLE) ||
                 (regionConfig->IsBufferable == MPU_ACCESS_NOT_BUFFERABLE));
    ASSERT_PARAM(regionConfig->SubRegionDisable <= (uint16_t)0x00FFU);
    ASSERT_PARAM(regionConfig->Size <= MPU_REGION_SIZE_4GB);

    MPU_REG_ConfigRegion(MPU, regionConfig);
}
#endif /* __MPU_PRESENT */

/*******EOF********************************************************************/
